﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Surface;
using Microsoft.Surface.Core;

namespace Sublimation
{
    public class EndScreenSurf:EndScreen
    {
        protected ReadOnlyContactCollection contacts;
        int previous=0;
        protected Rectangle bCompeteBottom, bCompeteTop, bInstructionsBottom, bInstructionsTop, bHighscoresBottom, bHighscoresTop, bBackBottom, bBackTop, bSwitchTutoLeftBottom, bSwitchTutoLeftTop, bSwitchTutoRightBottom, bSwitchTutoRightTop;
        public EndScreenSurf(WorldPropertiesSurf worldproperties):base(worldproperties)
        {
            bCompeteBottom = new Rectangle(220, 480, 320, 100);
            bCompeteTop = new Rectangle(480, 200, 320, 100);

            bInstructionsBottom = new Rectangle(575, 470, 200, 60);
            bInstructionsTop = new Rectangle(220, 240, 200, 60);

            bHighscoresBottom = new Rectangle(587, 568, 200, 60);
            bHighscoresTop = new Rectangle(225, 150, 200, 60);

            bBackBottom = new Rectangle(700, 625, 200, 60);
            bBackTop = new Rectangle(180, 110, 200, 60);

            bSwitchTutoLeftBottom = new Rectangle(250, 460, 50, 40);
            bSwitchTutoLeftTop = new Rectangle(700 , 270, 50, 40);

            bSwitchTutoRightBottom = new Rectangle(630, 460, 50, 40);
            bSwitchTutoRightTop = new Rectangle(320, 270, 50, 40);
        }
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            contacts = ((WorldPropertiesSurf)worldProperties).Contacts;
            base.Update(gameTime);
            if (contacts == null)
            {
                return;
            }
            foreach (Contact contact in contacts)
            {
                Rectangle contactBounds = new Rectangle((int)contact.Bounds.Left,(int)contact.Bounds.Top,(int)contact.Bounds.Width,(int)contact.Bounds.Height);
                if (contact.Id == previous)
                {
                    continue;
                }
                switch (state)
                {
                    case ScreenStates.start :
                        if (bCompeteBottom.Intersects(contactBounds) || bCompeteTop.Intersects(contactBounds))
                        {
                            state = ScreenStates.inGame;
                            previous = contact.Id;
                        }
                        else if (bInstructionsBottom.Intersects(contactBounds) || bInstructionsTop.Intersects(contactBounds))
                        {
                            state = ScreenStates.tuto1;
                            previous = contact.Id;
                        }
                        else if (bHighscoresBottom.Intersects(contactBounds) ||  bHighscoresTop.Intersects(contactBounds))
                        {
                            state = ScreenStates.highscores;
                            previous = contact.Id;
                        }

                        break;
                    case ScreenStates.tuto1:                        
                        if (bBackBottom.Intersects(contactBounds)||  bBackTop.Intersects(contactBounds))
                        {
                            state = ScreenStates.start;
                            previous = contact.Id;
                        }
                        else if (bSwitchTutoLeftBottom.Intersects(contactBounds) || bSwitchTutoLeftTop.Intersects(contactBounds)||bSwitchTutoRightBottom.Intersects(contactBounds) || bSwitchTutoRightTop.Intersects(contactBounds))
                        {
                            state = ScreenStates.tuto2;
                            previous = contact.Id;
                        }
                        break;
                    case ScreenStates.tuto2:
                        if (bBackBottom.Intersects(contactBounds) || bBackTop.Intersects(contactBounds))
                        {
                            state = ScreenStates.start;
                            previous = contact.Id;
                        }
                        else if (bSwitchTutoLeftBottom.Intersects(contactBounds) || bSwitchTutoLeftTop.Intersects(contactBounds) || bSwitchTutoRightBottom.Intersects(contactBounds) || bSwitchTutoRightTop.Intersects(contactBounds))
                        {
                            state = ScreenStates.tuto1;
                            previous = contact.Id;
                        }
                        break;
                    case ScreenStates.highscores:
                        if (bBackBottom.Intersects(contactBounds) || bBackTop.Intersects(contactBounds))
                        {
                            state = ScreenStates.start;
                            previous = contact.Id;
                        }
                        break;
                    case ScreenStates.end:
                        if (bBackBottom.Intersects(contactBounds) || bBackTop.Intersects(contactBounds))
                        {
                            worldProperties.Game.Exit();
                            previous = contact.Id;
                        }
                        break;
                }
            }
        }
    }
}



